/**
 * 
 */
package org.mspring.mlog.web.module.admin;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.NewsCatalog;
import org.mspring.mlog.service.NewsCatalogService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.query.NewsCatalogQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 
 * @author Gao Youbo
 * @since 2013年6月28日
 */
@Widget
@RequestMapping("/admin/news/catalog")
public class Admin_NewsCatalogWidget extends Admin_AbstractWidget {
    @Autowired
    private NewsCatalogService newsCatalogService;

    @RequestMapping("/list")
    @Log
    public String list(@ModelAttribute Page<NewsCatalog> newsCatalogPage, @ModelAttribute NewsCatalog newsCatalog, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (newsCatalogPage == null) {
            newsCatalogPage = new Page<NewsCatalog>();
        }
        newsCatalogPage = newsCatalogService.listPage(new NewsCatalogQueryCriterion(queryParams), newsCatalogPage);
        model.addAttribute("newsCatalogPage", newsCatalogPage);
        return "/admin/news/catalog/listNewsCatalog";
    }

    /**
     * 创建分类页面
     * 
     * @param newsCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/create")
    public String create(@ModelAttribute NewsCatalog newsCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "/admin/news/catalog/createNewsCatalog";
    }

    /**
     * 创建分类
     * 
     * @param newsCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/create/save")
    @Log
    public String create_save(@ModelAttribute NewsCatalog newsCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (newsCatalog.getCreateTime() == null) {
            newsCatalog.setCreateTime(new Date());
        }
        newsCatalogService.save(newsCatalog);
        return "redirect:/admin/news/catalog/list";
    }

    /**
     * 修改页面
     * 
     * @param newsCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(@ModelAttribute NewsCatalog newsCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (newsCatalog == null || newsCatalog.getId() == null) {
            Long id = SessionUtils.getAttrAsLong(request, "Admin_NewsCatalogWidget_edit_id");
            if (id != null) {
                newsCatalog = new NewsCatalog(id);
            }
        }
        SessionUtils.setAttr(request, "Admin_NewsCatalogWidget_edit_id", newsCatalog.getId());

        if (newsCatalog == null || newsCatalog.getId() == null) {
            return prompt(model, "请先选择要修改的分类");
        }
        newsCatalog = newsCatalogService.get(newsCatalog.getId());
        model.addAttribute("newsCatalog", newsCatalog);
        return "/admin/news/catalog/editNewsCatalog";
    }

    /**
     * 执行修改操作
     * 
     * @param newsCatalog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@ModelAttribute NewsCatalog newsCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        newsCatalogService.update(newsCatalog);
        return "redirect:/admin/news/catalog/list";
    }

    @RequestMapping("/delete")
    @Log
    public String delete(@RequestParam(required = false) Long[] id, @ModelAttribute Page<NewsCatalog> newsCatalogPage, @ModelAttribute NewsCatalog newsCatalog, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id != null && id.length > 0) {
            newsCatalogService.remove(id);
        }
        return list(newsCatalogPage, newsCatalog, queryParams, request, response, model);
    }
}
